import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)
const myIsLoading = {
	state: {
		isLoading: false
	},
	mutations: {
		updateLoadingStatus(state, payload) {
			state.isLoading = payload.isLoading
		}
	}
}

const store = new Vuex.Store({
	modules: {
		myIsLoading
	},
	state: {
		isAuth: false,
		msg: '',
		user: '',
		pwd: '',
		type: ''
	},
	mutations: {
		LoginByUsername(state, payload) {
			state.isAuth = payload.isAuth
		}
	},
	actions: {

		LoginByUsername({
			commit
		}, userInfo) {
			return new Promise((resolve, reject) => {
				axios.post('/api/user/login', {
					user: userInfo.user,
					pwd: userInfo.pwd
				}).then(res => {
					resolve(res)
				})
			})
		},
		register({
			commit
		}, userInfo) {
			return new Promise((resolve, reject) => {
				axios.post('/api/user/register', {
					user: userInfo.user,
					pwd: userInfo.pwd,
					type: userInfo.type
				}).then(res => {
					resolve(res)
				})
			})
		}
	}
	// modules: {
	// 	myIsLoading,
	// 	LoginByUsername
	// }
})

export default store
